package com.laicunba.service.gaoda.impl;

import org.apache.log4j.Logger;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.ApplicationEventPublisherAware;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.laicunba.event.UserCreateEvent;
import com.laicunba.pojo.UserDO;
import com.laicunba.service.UserBankService;
import com.laicunba.service.UserService;
import com.laicunba.service.gaoda.GaodaService;
import com.laicunba.service.gaoda.vo.GaodaUserVO;
import com.laicunba.util.AssertUtil;
import com.laicunba.util.DateUtil;
import com.laicunba.util.HttpUtil;
import com.laicunba.util.StringUtil;

@Service
public class GaodaServiceImpl implements GaodaService, ApplicationEventPublisherAware {
	private static final Logger LOG = Logger.getLogger(GaodaServiceImpl.class);
	private final static String URL_GET_USER_INFO = "http://www.laicunba.com/exchangeP2P-wx/userMsgAction!getGaodaUser.htm";
	private ApplicationEventPublisher publisher;
	@Autowired
	private UserService userService;
	@Autowired
	private UserBankService userBankService;

	@Override
	public GaodaUserVO getUser(String gaodaToken) {

		try {
			String rs = HttpUtil.doGet(URL_GET_USER_INFO + "?gaodaToken=" + gaodaToken);
			if (StringUtil.isEmpty(rs) || !rs.startsWith("{")) {
				return null;
			}
			JSONObject json = (JSONObject) new JSONParser().parse(rs);
			String userJsonStr = (String) json.get("data");
			if (StringUtil.isEmpty(userJsonStr) || !userJsonStr.startsWith("{")) {
				return null;
			}
			JSONObject userJson = (JSONObject) new JSONParser().parse(userJsonStr);
			if (userJson != null) {
				GaodaUserVO gaodaUserVO = new GaodaUserVO();
				gaodaUserVO.setRealName((String) userJson.get("realName"));
				gaodaUserVO.setUserState((String) userJson.get("userState"));
				gaodaUserVO.setInvitationCode((String) userJson.get("invitationCode"));
				gaodaUserVO.setUserId((String) userJson.get("userId"));
				gaodaUserVO.setInviterCode((String) userJson.get("inviterCode"));
				gaodaUserVO.setUserName((String) userJson.get("userName"));
				gaodaUserVO.setUserPhone((String) userJson.get("userPhone"));
				gaodaUserVO.setLoginPwd((String) userJson.get("loginPwd"));
				gaodaUserVO.setBankCardNum((String) userJson.get("bankCardNum"));
				gaodaUserVO.setBankName((String) userJson.get("bankName"));
				gaodaUserVO.setIDCardNum((String) userJson.get("IDCardNum"));
				gaodaUserVO.setUserCode((String) userJson.get("userCode"));
				String regeistDate = (String) userJson.get("registDate");
				gaodaUserVO.setRegistDate(DateUtil.parse(regeistDate));
				return gaodaUserVO;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	@Transactional
	@Override
	public boolean syn2LocalDB(GaodaUserVO gaodaUserVO) {

		/*
		AssertUtil.assertNotNull(gaodaUserVO);
		UserDO userDO = userService.findByMobile(gaodaUserVO.getUserPhone());
		if (userDO != null) {
			return false;
		}

		userDO = new UserDO();
		userDO.setId(gaodaUserVO.getUserCode());
		userDO.setMobile(gaodaUserVO.getUserPhone());
		userDO.setLoginName(gaodaUserVO.getUserName());
		userDO.setLoginPwd(gaodaUserVO.getLoginPwd());
		userDO.setPid(gaodaUserVO.getIDCardNum());
		userDO.setTrueName(gaodaUserVO.getRealName());
		userDO.setInviteCode(gaodaUserVO.getInvitationCode());
		userDO.setInviteFrom(gaodaUserVO.getInviterCode());
		AssertUtil.assertTrue(userService.create(userDO), "CREATE_USER_ERROR");

		UserCreateEvent event = new UserCreateEvent(userDO);
		try {
			publisher.publishEvent(event);
		} catch (Exception e) {
			LOG.error(null, e);
		}
	*/
		// if (StringUtil.isNotEmpty(gaodaUserVO.getBankCardNum()) &&
		// StringUtil.isNotEmpty(gaodaUserVO.getBankName()) &&
		// StringUtil.isNotEmpty(gaodaUserVO.getIDCardNum()) &&
		// StringUtil.isNotEmpty(gaodaUserVO.getRealName()) &&
		// StringUtil.isNotEmpty(gaodaUserVO.getUserPhone())) {
		// UserBankDO userBankDO = new UserBankDO();
		// userBankDO.setUserId(userDO.getId());
		// userBankDO.setBankCardNo(gaodaUserVO.getBankCardNum());
		// userBankDO.setBankName(gaodaUserVO.getBankName());
		// userBankDO.setMobile(gaodaUserVO.getUserPhone());
		// userBankDO.setPid(gaodaUserVO.getIDCardNum());
		// userBankDO.setHolder(gaodaUserVO.getRealName());
		//
		// AssertUtil.assertTrue(userBankService.create(userBankDO),
		// "CREATE_USER_BANK_ERROR");
		// }

		return true;
	}

	@Override
	public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) {
		publisher = applicationEventPublisher;

	}
}
